#include <vector>
#include <algorithm>

using namespace std;

class Solution
{
public:
    int f[210][210];
    int maxValue(vector<vector<int>> &grid)
    {
        int m = grid.size(), n = grid[0].size();
        f[0][0] = grid[0][0];
        for (int i = 0; i < m; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                if (i == 0 && j == 0)
                {
                    continue;
                }
                if (i == 0)
                {
                    f[i][j] = f[i][j - 1] + grid[i][j];
                }
                else if (j == 0)
                {
                    f[i][j] = f[i - 1][j] + grid[i][j];
                }
                else
                {
                    f[i][j] = max(f[i - 1][j], f[i][j - 1]) + grid[i][j];
                }
            }
        }
        return f[m - 1][n - 1];
    }
};

//滚动数组优化

class Solution
{
public:
    int f[210];
    int maxValue(vector<vector<int>> &grid)
    {
        int m = grid.size(), n = grid[0].size();
        for (int i = 1; i <= m; ++i)
        {
            for (int j = 1; j <= n ;++j)
            {
                f[j] = max(f[j - 1], f[j]) +grid[i - 1][j - 1];
            }
        }
        return f[n];
    }
};